package com.sun.controller;

import com.sun.pojo.entity.User;
import com.sun.pojo.vo.UserVo;
import com.sun.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author SunRui
 * @description TODO
 * @date 2024/10/5 16:05
 */
@Controller
@ResponseBody
@RequestMapping(value = "/user")
public class UserController {
    @Autowired
    UserService userService;

    /*
    获取全部用户
    */
    @GetMapping("getuserList")
    public List<UserVo> getUserList(String name,Integer gender){
        System.out.println("接收的数据:name="+name+";gender="+gender);
        List<UserVo> userVos = userService.getUserList(name,gender);
        return userVos;
    }

    /*
    获取用户详情
     */
    @GetMapping("getUserInfo")
    public List<UserVo> getUserInfo(Integer id){
        System.out.println("接收的Id="+id);
        List<UserVo> userVo = userService.getUserInfo(id);
        return userVo;
    }

    /*
    修改用户信息
     */
    @PostMapping("updateUser")
    public String updateUser(@RequestBody User user){
        System.out.println("接收的数据信息:"+user.toString());
        if (userService.updateUser(user)>0){
            System.out.println("数据修改成功!");
            return "数据修改成功";
        }
        return "数据修改失败";
    }

    /*
    新增用户
     */
    @PostMapping("addUser")
    public String addUser(@RequestBody User user){
        System.out.println("接收的数据信息:"+user.toString());
        if (userService.addUser(user)>0){
            System.out.println("数据新增成功!");
            return "数据新增成功";
        }
        return "数据新增失败";
    }

    /*
    删除用户
     */
    @PostMapping("deleteUser")
    public String deleteUser(Integer id){
        System.out.println("接收的Id:"+id);
        if (userService.deleteUser(id)>0){
            System.out.println("数据删除成功!");
            return "数据删除成功";
        }
        return "数据删除失败";
    }
}
